/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.systemView;
/**
 * 说明：
 * 角色权限管理
 * @Author：李智深
 * @Date：2012-05-04
 */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import com.ruipengkj.bean.TRight;
import com.ruipengkj.bean.TRole;
import com.ruipengkj.bean.TRoleRight;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.service.TRightService;
import com.ruipengkj.service.TRoleRightService;
import com.ruipengkj.service.TRoleService;
import com.ruipengkj.view.base.BaseDialog;

public class SystemManagePrivilegeAddDialog  extends BaseDialog implements Runnable{
	
	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent  nameTF;//角色名称
	private JLabelJTextAreaComponent descTF;//描述
	private JButton sureButton;//确定
	private JList leftList;
	private DefaultListModel leftListModel;
	private JList rigthList;
	private DefaultListModel rigthListModel;
	private JButton selectAllButton;//选中待选中所有的数据
	private JButton selectButton;//选中待选中的数据
	private JButton delSelectButton;//移除选定的数据
	private JButton delSelectAllButton;//移除选定所有的数据
	private TRoleService tRoleService;//角色
	private TRightService tRightService;//权限
	private TRoleRightService tRoleRightService;//角色权限
	public boolean isSuccess=false;
	public SystemManagePrivilegeAddDialog(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		tRoleService=BuildSpringApplication.getInstance().getTRoleServiceImpl();
		tRightService=BuildSpringApplication.getInstance().getTRightServiceImpl();
		tRoleRightService=BuildSpringApplication.getInstance().getTRoleRightServiceImpl();
		this.setSize(700, 450);
		this.setLocationRelativeTo(null);
		this.init();
		this.initListener();
	}
	private void init() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp, BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		nameTF = new JLabelJTextFieldComponent("角色名称：", 40);
		descTF=new JLabelJTextAreaComponent("描    述：", 35, 2);
		main_jp.add(nameTF);
		main_jp.add(descTF);
		this.add(main_jp, BorderLayout.NORTH);
		this.add(center(), BorderLayout.CENTER);
		this.add(initDown(), BorderLayout.SOUTH);
	}
	
	private JPanel initDown() {
		JPanel down_panel = new JPanel();
		sureButton = new JButton("确定(Ctrl+S)");
		down_panel.add(sureButton);
		return down_panel;
	}
	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		delSelectAllButton.addActionListener(this);
		selectAllButton.addActionListener(this);
		selectButton.addActionListener(this);
		delSelectButton.addActionListener(this);
		delSelectAllButton.addActionListener(this);
		leftList.addMouseListener(this);
		rigthList.addMouseListener(this);
	}
	
	private JPanel center(){
		leftListModel = new DefaultListModel();
		leftList = new JList(leftListModel);
		leftList.setFixedCellHeight(25);
		leftList.setFixedCellWidth(250);
		leftList.setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		leftList.setVisibleRowCount(10);
		JScrollPane waitSelectJsp = new JScrollPane(leftList);
		
		JPanel jp=new JPanel();
		BoxLayout box=new BoxLayout(jp, BoxLayout.Y_AXIS);
		jp.setLayout(box);
		selectAllButton = new JButton(">>");
		selectButton = new JButton(">");
		delSelectButton = new JButton("<");
		delSelectAllButton = new JButton("<<");
		jp.add(selectAllButton);
		jp.add(selectButton);
		jp.add(delSelectButton);
		jp.add(delSelectAllButton);
		
		rigthListModel = new DefaultListModel();
		rigthList = new JList(rigthListModel);
		rigthList.setFixedCellHeight(25);
		rigthList.setFixedCellWidth(250);
		rigthList.setVisibleRowCount(10);
		rigthList.setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		JScrollPane selectJsp = new JScrollPane(rigthList);
		return new JPanelFlowLayoutComponent(new Component[]{waitSelectJsp, jp, selectJsp
		}, FlowLayout.CENTER);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==sureButton){
			//确定
			String name=nameTF.getText().getText().trim();
			String desc=descTF.getArea().getText();
			if(name.equals("")||name.length()>100){
				JOptionPane.showMessageDialog(null, "角色名称不能为空，且不能大于100个字");
				nameTF.getText().requestFocus();
				return;
			}
			if(desc.equals("")||desc.length()>200){
				JOptionPane.showMessageDialog(null, "角色描述不能为空，且不能大于200个字");
				descTF.getArea().requestFocus();
				return;
			}
			int size=rigthListModel.getSize();
			if(size<=0){
				JOptionPane.showMessageDialog(null, "角色权限不能为空，请为该角色添加权限");
				return;
			}
			TRole role=new TRole();
			role.setRoleID(UUID.GenUUID15());
			role.setName(name);
			role.setDes(desc);
			try {
				if(tRoleService.findRoleByName(name)!=null){
					JOptionPane.showMessageDialog(null, "该权限名称已经存在，请重新输入");
					return;
				}
				tRoleService.saveService(role);
				for(int i=0;i<size;i++){
					Object obj1=rigthListModel.elementAt(i);
					TRoleRight roleRight=new TRoleRight();
					roleRight.setTRole(role);
					roleRight.setTRight((TRight)obj1);
					tRoleRightService.saveService(roleRight);
				}
				this.isSuccess=true;
				BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加角色信息");
				JOptionPane.showMessageDialog(null, "添加成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "添加失败");
			}
			this.dispose();
		}else if(e.getSource()==selectAllButton){
			//将所有数据移动
			int size=leftListModel.getSize();
			for(int i=0;i<size;i++){
				Object obj1=leftListModel.elementAt(i);
				rigthListModel.addElement(obj1);
			}
			leftListModel.removeAllElements();
		}else if(e.getSource()==selectButton){
			//将选中的所有数据移动
			LeftToRigthSelect();
		}else if(e.getSource()==delSelectButton){
			//将选中的所有数据移除
			RigthToLeftSelect();
		}else if(e.getSource()==delSelectAllButton){
			//将所有数据移除
			int size=rigthListModel.getSize();
			for(int i=0;i<size;i++){
				Object obj1=rigthListModel.elementAt(i);
				leftListModel.addElement(obj1);
			}
			rigthListModel.removeAllElements();
		}
	}
	
	//将选中的所有数据移动
	private void LeftToRigthSelect() {
		int[] ins=leftList.getSelectedIndices();
		if(ins!=null&&ins.length!=0){
			for(int i=0;i<ins.length;i++){
				Object obj1=leftListModel.elementAt(ins[i]);
				rigthListModel.addElement(obj1);
			}
			for(int i=0;i<ins.length;i++){
				leftListModel.remove(ins[i]-i);
			}
		}
	}
	
	//将选中的所有数据移除
	private void RigthToLeftSelect() {
		int[] ins=rigthList.getSelectedIndices();
		if(ins!=null&&ins.length!=0){
			for(int i=0;i<ins.length;i++){
				Object obj1=rigthListModel.elementAt(ins[i]);
				leftListModel.addElement(obj1);
			}
			for(int i=0;i<ins.length;i++){
				rigthListModel.remove(ins[i]-i);
			}
		}
	}
	@Override
	public void run() {
		try {
			List<TRight> list=tRightService.findService(null);
			if(list!=null&&list.size()!=0){
				for(int i=0;i<list.size();i++){
					leftListModel.addElement(list.get(i));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void showDialog(){
		this.isSuccess=false;
		SwingUtilities.invokeLater(this);
		this.setVisible(true);
	}

	//鼠标点击事件
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() == 2) {
			if (e.getSource() == leftList) {
				LeftToRigthSelect();
			} else if (e.getSource() == rigthList) {
				RigthToLeftSelect();
			}
		}
	}
}
